package com.item3;

import android.content.Context;
import android.os.Bundle;
import android.os.Looper;
import android.os.ParcelUuid;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.fragment.app.Fragment;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @Author WXX
 * @Date 2020/7/19 12:07
 * @Version 1.0
 */
public abstract class LazyFragment extends Fragment {
    private Context mContext;
    public boolean isFirstLoad = true; // 是否第一次加载
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        Log.e("父类", "onCreate: " );
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(mContext).inflate(getContentViewId(), null);
        initView(view);
        Log.e("父类", "onCreateView: " );
        return view;
    }

    //ViewPager适配Fragment时，刷新一次页面就会调用这个方法一次
    @Override
    public void onResume() {
        super.onResume();
        Log.e("父类", "onResume: " );
        if (isFirstLoad) {
            // 将数据加载逻辑放到onResume()方法中
            Log.e("第一次加载", "onResume: "+"加载数据" );
            initData();
            initEvent();
            isFirstLoad = false;
        }
    }

    /**
     * 设置布局资源id
     *
     * @return
     */
    protected abstract int getContentViewId();

    /**
     * 初始化视图
     *
     * @param view
     */
    protected void initView(View view) {

    }

    /**
     * 初始化数据
     */
    protected  void initData(){
        Toast.makeText(getActivity(),"加载数据......",Toast.LENGTH_SHORT).show();
        //加载数据


    }

    /**
     * 初始化事件
     */
    protected void initEvent() {

    }

    //第一次创建Fragment时会在创建后执行一次启动方法，有且仅有一次
    @Override
    public void onStart() {
        super.onStart();
        Log.e("父类", "onStart: " );
    }
}
